package com.guomw.report.web.controller;


import com.guomw.report.common.enums.AppCode;
import com.guomw.report.common.utils.ApiResult;
import com.guomw.report.service.entity.DetectionIndex;
import com.guomw.report.service.entity.Report;
import com.guomw.report.service.repository.DetectionIndexRepository;
import com.guomw.report.service.service.DetectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/detection")
public class DetectionController {

    @Autowired
    private DetectionIndexRepository detectionIndexRepository;

    @Autowired
    private DetectionService detectionService;

    @RequestMapping("/list/{reportId}")
    public String list(@PathVariable(name = "reportId") Long reportId, Model model) {
        model.addAttribute("reportId", reportId);
        return "detection/list";
    }


    @RequestMapping("/listView")
    @ResponseBody
    public ApiResult listView(@RequestParam(required = false, defaultValue = "") String code,
                              @RequestParam(required = false, defaultValue = "1") Integer pageIndex
            , Long reportId) {

        return ApiResult.resultWith(AppCode.SUCCESS, detectionService.searchList(reportId, code, pageIndex));
    }

    @RequestMapping("/add/{reportId}")
    public String add(@PathVariable(name = "reportId") Long reportId, Model model) {
        DetectionIndex detectionIndex = new DetectionIndex();
        detectionIndex.setSort(100);
        detectionIndex.setReportId(reportId);
        detectionIndex.setId(0);
        model.addAttribute("detection", detectionIndex);
        return "detection/edit";
    }


    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable(name = "id", required = false) Integer id, Model model) {
        if (id != null) {
            model.addAttribute("detection", detectionIndexRepository.findOne(id));
        }
        return "detection/edit";
    }


    @RequestMapping("/save")
    @ResponseBody
    public ApiResult save(Integer id, String code, Long reportId, String normalExplain, String abNormalExplain, Integer sort) {

        if (!StringUtils.isEmpty(code)) {
            DetectionIndex detection = detectionIndexRepository.findOne(id);
            if (detection != null) {
                detection.setReportId(reportId);
                detection.setCode(code);
                detection.setNormalExplain(normalExplain);
                detection.setAbNormalExplain(abNormalExplain);
                detection.setSort(sort);
                detectionIndexRepository.save(detection);
            } else {
                //判断是否存在相同的code
                List<DetectionIndex> detectionIndexList = detectionIndexRepository.findByReportIdAndCode(reportId, code);
                if (detectionIndexList != null && detectionIndexList.size() > 0) {
                    return ApiResult.resultWith(AppCode.EXIST_SAME_CODE);
                }
                detection = new DetectionIndex();
                detection.setReportId(reportId);
                detection.setCode(code);
                detection.setNormalExplain(normalExplain);
                detection.setAbNormalExplain(abNormalExplain);
                detection.setSort(sort);
                detectionIndexRepository.save(detection);
            }

        }
        return ApiResult.resultWith(AppCode.SUCCESS);
    }

}
